import 'package:flutter/cupertino.dart';

import 'alert_action.dart';
import 'sheet_action.dart';

class AlertUtil {
  /// AlertUtil: 显示弹窗
  /// * [content] 内容文字
  /// * [titleWidget] 标题
  /// * [title] 标题文字
  /// * [contentWidget] 内容
  /// * [cancelWidget] 取消按钮
  /// * [cancel] 取消按钮文字
  /// * [confirmWidget] 确定按钮
  /// * [confirm] 确定按钮文字
  /// * [onCancel] 取消按钮回调
  /// * [onConfirm] 确定按钮回调
  /// * [isShowCancel] 是否显示取消按钮
  static void showAlert(
    BuildContext context, {
    required String content,
    Widget? titleWidget,
    String title = '提示',
    Widget? contentWidget,
    Widget? cancelWidget,
    String cancel = '取消',
    bool isCancelDestructiveAction = false,
    Widget? confirmWidget,
    String confirm = '确定',
    bool isConfirmDestructiveAction = false,
    VoidCallback? onCancel,
    VoidCallback? onConfirm,
    bool isShowCancel = true,
  }) {
    showCupertinoDialog(
      context: context,
      builder: (context) {
        return CupertinoAlertDialog(
          title: titleWidget ?? Text(title),
          content: contentWidget ?? Text(content),
          actions: isShowCancel
              ? [
                  AlertUtilAlertAction(
                    widget: cancelWidget,
                    text: cancel,
                    isDestructiveAction: isCancelDestructiveAction,
                    callback: onCancel,
                  ),
                  AlertUtilAlertAction(
                    widget: confirmWidget,
                    text: confirm,
                    isDestructiveAction: isConfirmDestructiveAction,
                    callback: onConfirm,
                  ),
                ]
              : [
                  AlertUtilAlertAction(
                    widget: confirmWidget,
                    text: confirm,
                    isDestructiveAction: isConfirmDestructiveAction,
                    callback: onConfirm,
                  ),
                ],
        );
      },
    );
  }

  /// AlertUtil: 显示底部弹窗
  /// * [content] 内容文字
  /// * [titleWidget] 标题
  /// * [title] 标题文字
  /// * [contentWidget] 内容
  /// * [barrierDismissible] 点击空白取消（默认 true）
  /// * [actions] 按钮列表
  /// * [onCancel] 取消按钮回调
  /// * [isShowCancel] 是否显示取消按钮
  static void showSheet(
    BuildContext context, {
    required String content,
    Widget? titleWidget,
    String title = '',
    Widget? contentWidget,
    bool barrierDismissible = true,
    required List<AlertUtilSheetAction> actions,
    AlertUtilSheetAction? cancelAction,
    VoidCallback? onCancel,
    bool isShowCancel = true,
  }) {
    showCupertinoModalPopup(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (context) {
        return CupertinoActionSheet(
          title: titleWidget ?? Text(title),
          message: contentWidget ?? Text(content),
          cancelButton: cancelAction ?? (isShowCancel ? AlertUtilSheetAction(text: '取消', callback: onCancel) : null),
          actions: actions,
        );
      },
    );
  }
}


